import numpy as np
import matplotlib.pyplot as plt
# from python_ai.ML.lin_regression.xlib import *
from sklearn.linear_model import LinearRegression

data_loaded = np.loadtxt(r'../data/ex1data1.txt', delimiter=',')
m = len(data_loaded)
n = 2
print(m)
print(data_loaded[:5])
x = data_loaded[:, 0].reshape(m, 1)
y = data_loaded[:, 1].ravel()
print(x[:5])
print(y[:5])


model = LinearRegression()
# theta = regular_equation(x, y)
model.fit(x, y)
theta1_n = model.coef_
theta0 = model.intercept_
print('theta0 and theta1_n:', theta0, theta1_n)
print('theta0 and theta1_n shape:', theta0.shape, theta1_n.shape)
print('x, y shape', x.shape, y.shape)
theta = np.r_[theta0, theta1_n]
xscore = model.score(x, y)
print('sklearn')
print(f'THETA = {theta}')
print(f'score = {xscore}')

# plt_line_x = np.array([x.min(), x.max()]).ravel()
# plt_line_y = np.c_[np.ones([2, 1]), plt_line_x].dot(theta).ravel()
plt_line_x = x.ravel()
h = model.predict(x)
print('h.shape', h.shape)
plt_line_y = model.predict(x).ravel()

plt_data_x = x.ravel()
plt_data_y = y.ravel()
plt.scatter(plt_data_x, plt_data_y)
plt.plot(plt_line_x, plt_line_y, 'r-')
plt.show()
